package com.lanchewuyou.user.driver.lipeiui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

import com.afollestad.materialdialogs.MaterialDialog;
import com.lanchewuyou.user.driver.R;
import com.lanchewuyou.user.driver.Utils.TUtils;
import com.lanchewuyou.user.driver.modul.Config;
import com.lanchewuyou.user.driver.modul.PagerSlidingTabStrip;
import com.lanchewuyou.user.driver.ui.DriverInfoActivity;
import com.lanchewuyou.user.driver.ui.SigninActivity;
import com.umeng.update.UmengUpdateAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * 显示全部易理赔订单列表界面,用户已登录时直接进入此界面
 */

public class LiPeiMainActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private ViewPager viewPager;
    private List<Fragment> fragmentList;
    private PagerSlidingTabStrip mPagerSlidingTabStrip;
    private FragmentPagerAdapter fragmentPagerAdapter;
    private static String DRIVERID;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lipei_main);
        initViews();
        UmengUpdateAgent.setDeltaUpdate(true);
        UmengUpdateAgent.update(this);
        SharedPreferences sharedPreferences = getSharedPreferences(Config.PASSWORD_PATH, Activity.MODE_PRIVATE);
        DRIVERID = sharedPreferences.getString("driverid", "");
        //准备fragment
        fragmentList = new ArrayList<Fragment>();
        android.support.v4.app.Fragment fragment1 = new LipeiMainFragment1();
        android.support.v4.app.Fragment fragment2 = new LipeiMainFragment2();
        fragmentList.add(fragment1);
        fragmentList.add(fragment2);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentPagerAdapter = new FragmentPagerAdapter(fragmentManager) {
            private String[] Titles = {"未完成订单", "已完成订单"};

            @Override
            public Fragment getItem(int i) {
                return fragmentList.get(i);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return Titles[position];
            }

            @Override
            public int getCount() {
                return 2;
            }
        };
        viewPager = (ViewPager) findViewById(R.id.main_viewpager);
        viewPager.setAdapter(fragmentPagerAdapter);
        mPagerSlidingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.main_pagersliding);
        mPagerSlidingTabStrip.setViewPager(viewPager);
        mPagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int i) {
            }

            @Override
            public void onPageScrollStateChanged(int i) {
            }
        });
        initTabsValue();
    }

    private void initViews() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("易理赔订单");
        setSupportActionBar(mToolbar);
    }

    //TAB设置
    private void initTabsValue() {
        // 底部游标颜色
        mPagerSlidingTabStrip.setIndicatorColor(Color.parseColor("#eb8157"));
        // tab的分割线颜色
        mPagerSlidingTabStrip.setDividerColor(Color.TRANSPARENT);
        // tab背景
        mPagerSlidingTabStrip.setBackgroundColor(Color.parseColor("#dfdfdf"));
        // tab底线高度
        mPagerSlidingTabStrip.setUnderlineHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                1, getResources().getDisplayMetrics()));
        // 游标高度
        mPagerSlidingTabStrip.setIndicatorHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                5, getResources().getDisplayMetrics()));
        // 选中的文字颜色
        mPagerSlidingTabStrip.setSelectedTextColor(Color.BLACK);
        mPagerSlidingTabStrip.setShouldExpand(true);
        // 正常文字颜色d
        mPagerSlidingTabStrip.setTextColor(Color.parseColor("#7d7d7d"));
        mPagerSlidingTabStrip.setTextSize(40);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_guide, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        //登出账号
        if (id == R.id.login_out) {
            new MaterialDialog.Builder(this)
                    .title("您确定？")
                    .content("将登出您的账号")
                    .positiveText("确定")
                    .negativeText("取消")
                    .callback(new MaterialDialog.ButtonCallback() {
                        @Override
                        public void onPositive(MaterialDialog dialog) {
                            SharedPreferences preferences = getSharedPreferences(Config.PASSWORD_PATH, Activity.MODE_PRIVATE);
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.remove("drivername");
                            editor.remove("password");
                            editor.remove("mobile");
                            editor.remove("driverid");
                            editor.remove("driverpic");
                            editor.commit();
                            Intent intent = new Intent(LiPeiMainActivity.this, SigninActivity.class);
                            startActivity(intent);
                            finish();
                        }

                        @Override
                        public void onNegative(MaterialDialog dialog) {
                            super.onNegative(dialog);
                        }
                    })
                    .show();
        } else if (id == R.id.driver_info) {
            startActivity(new Intent(LiPeiMainActivity.this, DriverInfoActivity.class));
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private long exitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Object mHelperUtils;
                TUtils.showShort(LiPeiMainActivity.this, "再按一次退出程序");
                exitTime = System.currentTimeMillis();

            } else {
                finish();
            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }


}
